<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @submit.native.prevent="dataFormSubmit()" label-width="110px">
    <el-row :gutter='20'>
      <el-col :span="10">
        <el-form-item label="推荐邀请用户" prop="recommendUserId">
          <!-- <el-input v-model="dataForm.recommendUserId" placeholder="推荐用户id"></el-input> -->
          <el-select v-model="dataForm.recommendUserId" filterable placeholder="请选择">
            <el-option
              v-for="item in this.partner"
              :key="item.id"
              :label="item.userName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="升级支付金额" prop="recommendUpgradeMoney">
          <el-input v-model="dataForm.recommendUpgradeMoney" placeholder="升级支付金额" style="width: 200px"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter='20'>
      <el-col :span="10">
        <el-form-item label="推荐可得佣金" prop="recommendUserCommission">
          <el-input v-model="dataForm.recommendUserCommission" placeholder="推荐可得佣金" style="width: 200px"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="推荐可得积分" prop="recommendUpgradeGiveIntegral">
          <el-input v-model="dataForm.recommendUpgradeGiveIntegral" placeholder="推荐可得积分" style="width: 200px"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter='20'>
      <el-col>
        <el-form-item label="备注" prop="slogan">
          <el-input type="textarea" v-model="dataForm.slogan" placeholder="宣传内容" :autosize ="{ minRows: 4, maxRows: 8 }" style="width: 600px"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data: function () {
      var checkDecimal = (rule, value, callback) => {
        if (typeof value === 'object' || value === undefined) {
          value = rule.field
        }
        let reg = /^(^[0-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,}$)$/
        if (Number.parseFloat(value) <= 0 || !reg.test(value) || !Number.isFinite(Number.parseFloat(value))) {
          // callback(new Error('数据不合法'))
          callback(this.$message.error('佣金不规范！'))
        } else {
          if (Number.parseFloat(value) > Number.parseFloat(this.dataForm.recommendUpgradeMoney)) {
            callback(this.$message.error('佣金不得大于升级金额！'))
          } else {
            callback()
          }
        }
      }
      var checkDecimal1 = (rule, value, callback) => {
        if (typeof value === 'object' || value === undefined) {
          value = rule.field
        }
        let reg = /^(^[0-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,}$)$/
        if (Number.parseFloat(value) <= 0 || !reg.test(value) || !Number.isFinite(Number.parseFloat(value))) {
          // callback(new Error('数据不合法'))
          callback(this.$message.error('升级金额不规范！'))
        } else {
          callback()
        }
      }
      var checkNumber2 = (rule, value, callback) => {
        if (typeof value === 'object' || value === undefined) {
          value = rule.field
        }
        let reg = /^[0-9]\d*$/
        if (!reg.test(value)) {
          callback(this.$message.error('积分应为正整数！'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        partner: [],
        dataForm: {
          id: 0,
          slogan: '',
          recommendUserId: 0,
          recommendUserType: '',
          recommendUserCommission: '',
          recommendUpgradeMoney: '',
          recommendUpgradeGiveIntegral: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          slogan: [
            { required: true, message: '宣传内容不能为空', trigger: 'blur' }
          ],
          recommendUserId: [
            { required: true, message: '邀请用户不能为空', trigger: 'blur' }
          ],
          recommendUserCommission: [
            { validator: checkDecimal, required: true, trigger: 'blur' }
          ],
          recommendUpgradeMoney: [
            { validator: checkDecimal1, required: true, trigger: 'blur' }
          ],
          recommendUpgradeGiveIntegral: [
            { validator: checkNumber2, required: true, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.$http({
          url: this.$http.adornUrl('/eggUpgrade/queryAllPartnerList'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.partner = data.data
          }
        })
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eggUpgrade/getEggUpgradeInfoId/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.slogan = data.data.slogan
                this.dataForm.recommendUserId = data.data.recommendUserId
                this.dataForm.recommendUserCommission = data.data.recommendUserCommission
                this.dataForm.recommendUpgradeMoney = data.data.recommendUpgradeMoney
                this.dataForm.recommendUpgradeGiveIntegral = data.data.recommendUpgradeGiveIntegral
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eggUpgrade/${!this.dataForm.id ? 'addEggUpgrade' : 'modifyEggUpgradeById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'slogan': this.dataForm.slogan,
                'recommendUserId': this.dataForm.recommendUserId,
                'recommendUserCommission': this.dataForm.recommendUserCommission,
                'recommendUpgradeMoney': this.dataForm.recommendUpgradeMoney,
                'recommendUpgradeGiveIntegral': this.dataForm.recommendUpgradeGiveIntegral
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
